#ifndef PIPE_H_
#define PIPE_H_

#include <windows.h>

class Pipe {
private:
    HANDLE                  m_hRead[3];
    HANDLE                  m_hWrite[3];
    PROCESS_INFORMATION     m_childProcessInfo;
    BOOL                    m_openned;

    BOOL readFile(int fd, char* buf, DWORD len, DWORD& bytesRead);
    BOOL avail(int fd, DWORD& bytes);

public:
    Pipe();

    BOOL open(TCHAR* program, TCHAR* args);
    void close();

    BOOL isOpenned() { return m_openned; }

    BOOL read(char* buf, DWORD len, DWORD& bytesRead);
    BOOL readError(char* buf, DWORD len, DWORD& bytesRead);
    BOOL write(const char* buf, DWORD len, DWORD& byteWritten);
    BOOL readAvail(DWORD& bytes);
    BOOL readErrorAvail(DWORD& bytes);

    virtual ~Pipe() { close(); }
};

#endif  /* PIPE_H_ */

